/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import comu.Configuracio;
import controlador.autogenerats.ProducteJpaController;
import entity.Camp;
import entity.Producte;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author David
 */
public class ProducteControlador extends ProducteJpaController{

    public ProducteControlador() {
        super(Persistence.createEntityManagerFactory(Configuracio.PERSISTANCE_UNIT));
    }

    public ArrayList<Producte> findProducteEntitiesAZ() {
        EntityManager em = getEntityManager();
        try{
        Query q = em.createNativeQuery("select * from producte order by nom", Producte.class);
        Iterator it = q.getResultList().iterator();
        ArrayList<Producte> llista = new ArrayList<Producte>();
        while(it.hasNext()){
            llista.add((Producte) it.next());
        }
        return llista;
        } finally {
            em.close();
        }
    }

    public ArrayList<Producte> findProducteEntitiesAZWithProduccio() {
        EntityManager em = getEntityManager();
        try{
        Query q = em.createNativeQuery("select * from `producte` where `id` = any (select `productes_id` from `produccio`) order by `nom`", Producte.class);
        Iterator it = q.getResultList().iterator();
        ArrayList<Producte> llista = new ArrayList<Producte>();
        while(it.hasNext()){
            llista.add((Producte) it.next());
        }
        return llista;
        } finally {
            em.close();
        }
    }
}
